#!/usr/bin/perl

use strict;
use warnings;
use Data::Dumper;

&main;

# Detects the number of each nucleotide (a, c, g, t) in a given
# string and returns a hash with the info ('nucl' => nr of occurrences.)
# The input string may have any symbol, they will be silently discarded.
# Both lower and upper case are accepted, i.e., "ACGT" and "acgt" produce
# the same output.
sub gen_total_nucleotids
{
    my $nucl_seq = shift(@_);

    my %nucl_hash = ('a' => 0,
                     'c' => 0,
                     'g' => 0,
                     't' => 0);

    my @chars = split(//, $nucl_seq);
    
    for (my $i = 0; $i<=$#chars; $i++){

        # Ignore non-nucleotids
        if ($chars[$i] !~ m/a|c|g|t/i ){
            next;
        }
        $nucl_hash{lc($chars[$i])}++; # $chars[$i] is the i-th char of $nucl_seq
    }

    return %nucl_hash;
}


sub main
{
    if (not defined $ARGV[0]){
        die "I need a file from where to read the sequnce of nucleotides!\n";
    }
    
    open (HANDLE, $ARGV[0]) || die "can not open file $ARGV[0]: $!";

    my @lines = <HANDLE>;
    close (HANDLE);

    chomp @lines;

    my $nucl_seq = join('', @lines);
    my %nucls = gen_total_nucleotids($nucl_seq);

    print $nucls{'a'}." ".$nucls{'c'}." ".$nucls{'g'}." ".$nucls{'t'};
}

